package com.tangkun.environment.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tangkun.environment.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 删除对话框
 *
 * @author TangKun
 */
public class DeleteDialog extends Dialog {
    @BindView(R.id.tv_delete_title)
    TextView tvTitle;
    @BindView(R.id.tv_delete_content)
    TextView tvContent;
    @BindView(R.id.btn_delete_delete)
    Button btnDelete;
    private Context context;
    private View view;
    /**
     * 对话框中的文字
     */
    private String content = "";
    /**
     * 对话框中按钮上的文字
     */
    private String btnContent = "";

    public DeleteDialog(Context context) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        view = initView();
    }

    public DeleteDialog(Context context, int width, int height) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        view = initView();
    }

    /**
     * @param context 上下文
     * @param content 内容
     */
    public DeleteDialog(Context context, String content) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        this.content = content;
        view = initView();
    }

    /**
     * @param context    上下文
     * @param content    内容
     * @param btnContent 按钮上的文字
     */
    public DeleteDialog(Context context, String content, String btnContent) {
        super(context, R.style.InvitationDialogStyle);
        this.context = context;
        this.content = content;
        this.btnContent = btnContent;
        view = initView();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        requestWindowFeature(Window.PROGRESS_INDETERMINATE_ON);
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getWindow().setWindowAnimations(R.style.InvitationDialogStyle);
        view = initView();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dm.widthPixels, dm.heightPixels);
        addContentView(view, lp);
        setCanceledOnTouchOutside(true);
    }


    private View initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_delete, null);
        ButterKnife.bind(this, view);
        if (!TextUtils.isEmpty(content)) {
            tvContent.setText(content);
        } else {
            //ignore
        }
        if (!TextUtils.isEmpty(btnContent)) {
            btnDelete.setText(btnContent);
        } else {

        }
        return view;
    }

    @OnClick(R.id.btn_delete_delete)
    public void onClick() {
        dismiss();
        if (listener != null) {
            listener.onClick();
        }
    }

    public void setDeleteListener(DeleteListener listener) {
        this.listener = listener;
    }

    public DeleteListener listener;

    public interface DeleteListener {
        void onClick();
    }
}
